import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/cookie'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/tims/login',
      name: 'login',
      component: () => import('@/views/Login.vue')
    },
    {
      path:"/",
      redirect:'/tims/login',
    },
    {
      path: '/tims/courseDetails',
      name: 'courseDetails',
      component: () => import('@/views/course/CourseDetails.vue')
    },
    {
      path: '/tims/playBack',
      name: 'playBack',
      component: () => import('@/views/PlayBack.vue')
    },
    {
      path: '/tims/layout',
      name: 'layout',
      component: () => import('@/views/Layout.vue'),
      children: [
        {
          path: 'announcement',
          name: 'announcement',
          component: () => import('@/views/Announcement.vue'),
          meta: { activeMenu: 'announcement' }
        },
        {
          path: 'student',
          name: 'student',
          component: () => import('@/views/Student.vue'),
          meta: { activeMenu: 'student' }
        },
        {
          path: 'classroom',
          name: 'classroom',
          component: () => import('@/views/Classroom.vue'),
          meta: { activeMenu: 'classroom' }
        },
        {
          path: 'course',
          name: 'course',
          component: () => import('@/views/Course.vue'),
          meta: { activeMenu: 'course' }
        },
        {
          path: 'suser',
          name: 'suser',
          component: () => import('@/views/Suser.vue'),
          meta: { activeMenu: 'suser' }
        },
        {
          path: 'teacher',
          name: 'teacher',
          component: () => import('@/views/Teacher.vue'),
          meta: { activeMenu: 'teacher' }
        },
        {
          path: 'classroomMange',
          name: 'classroomMange',
          component: () => import('@/views/ClassroomMange.vue'),
          meta: { activeMenu: 'classroom' }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (getToken()) {  //已经登陆过
    /* has token*/
    if (to.path === '/tims/login') {
      next({ path: '/tims/layout/suser' })
    } else {
      next()
    }
  } else {
    if (to.path !== '/tims/login') {
      next({path:'/tims/login'})
    } else {
      next()
    }
  }
})

export default router


